/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JPanel;
import model.Clustering;
import model.Helper;
import model.InitRandomPoints;
import model.KMeans;
import model.Point;
import model.kruskal.Kruskal;
import view.PointsGUI;
import view.PointsPanel;
import view.StartGUI;

/**
 *
 * @author Stupi
 */
public class Controller {
    
    private StartGUI startGUI;
    private Clustering clustering;
    private PointsGUI pointsBefore;
    private PointsGUI pointsAfter;
    private InitRandomPoints initPoints;
    private JPanel pnlBefore;
    private PointsPanel pnlAfter;
    private LinkedList<Point> [] clusters;
    
    
    
    public Controller( ) {
        
        startGUI = new StartGUI();
        
        startGUI.addStartButtonListener(new StartListener());
    }
    
    class StartListener implements  ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            
            int numOfClusters = startGUI.getNumOfClusters();
            int numOfPoints = 0;
            
            try {
            numOfPoints = startGUI.getNumOfPoints();
            } catch (Exception e) {
                startGUI.showErrorMessage("Enter only integer vaues.");
                return;
            }
            int widthGUI = numOfPoints/2;
            int heightGUI = numOfPoints/2;
            if (heightGUI < 150) {
                heightGUI = 150;
            }
            if (widthGUI < 150) {
                widthGUI = 150;
            }
            
            
            initPoints = new InitRandomPoints();
            initPoints.initializePoints(numOfPoints, widthGUI, heightGUI);
            
            String typeOfAlg = startGUI.getTypeOfAlg();

            
            int [] xBefore = getXfromPoints();
            int [] yBefore = getYfromPoints();
            int [] limits = new int [1];
            limits [0] = xBefore.length;
            

            pointsBefore = new PointsGUI(widthGUI+20, heightGUI+30, xBefore, yBefore, limits);
            pointsBefore.setVisible(true);
            
            pointsBefore.setSize(widthGUI+20, heightGUI+30);
            
            
           
            
            
            if (typeOfAlg.equals("Kruskal algorithm")) {
                
                Kruskal kruskal = new Kruskal(numOfClusters, numOfPoints);
                kruskal.cluster();
                int [] xCoo = clusterAndGetXCoordinates(kruskal, numOfPoints);
                int [] yCoo = clusterAndGetYCoordinates(kruskal, numOfPoints);
                int [] lim = Helper.createArrayLimit(kruskal.getClusters());
                
                pointsAfter = new PointsGUI(widthGUI+20, heightGUI+30, xCoo, yCoo, lim);

                pointsAfter.setVisible(true);

                
            }   
            if (typeOfAlg.equals("K-Means algorithm")) {
                
                KMeans kmeans = new KMeans(widthGUI, heightGUI, numOfClusters);
                kmeans.cluster();
                int [] xCoo = clusterAndGetXCoordinates(kmeans, numOfPoints);
                int [] yCoo = clusterAndGetYCoordinates(kmeans, numOfPoints);
                int [] lim = Helper.createArrayLimit(kmeans.getClusters());
                
                
                pointsAfter = new PointsGUI(widthGUI+20, heightGUI+30, xCoo, yCoo, lim);

                pointsAfter.setVisible(true);

                
            }
           
            
            
             
 
        }
    
        
        public int[] getXfromPoints () {
            Point [] points = initPoints.getPoints();
            int [] xCoordinates = new int [points.length];
            
            for (int i = 0; i < points.length; i++) {
                xCoordinates [i] = points[i].getX();
            }
            
            return xCoordinates;
        }
    
         public int[] getYfromPoints () {
            Point [] points = initPoints.getPoints();
            int [] yCoordinates = new int [points.length];
            
            for (int i = 0; i < points.length; i++) {
                yCoordinates [i] = points[i].getY();
            }
            
            return yCoordinates;
        }
        
         
         public int [] clusterAndGetXCoordinates (Clustering type, int numOfPoints) {
             
             int [] coordinates = Helper.clustersToArrayXvalues(type.getClusters(), numOfPoints);
             

             
             
             return coordinates;
         }
         
          public int [] clusterAndGetYCoordinates (Clustering type, int numOfPoints) {
             
             int [] coordinates = Helper.clustersToArrayYvalues(type.getClusters(), numOfPoints);
             
             return coordinates;
         }
         
    }
   
    
    
    
}
